/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.harmony.recyclerview.custom;

import ohos.interwork.utils.ParcelableEx;

import ohos.utils.Parcel;
import ohos.utils.ParcelException;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * AbsSavedState class
 */
public class AbsSavedState implements ParcelableEx {
    /**
     * Initialize the AbsSavedState
     */
    public static final AbsSavedState EMPTY_STATE = new AbsSavedState() {
    };

    private ParcelableEx mSuperState;

    /**
     * Constructor used to make the EMPTY_STATE singleton
     */
    private AbsSavedState() {
        mSuperState = null;
    }

    /**
     * Constructor called by derived classes when creating their SavedState objects
     *
     * @param superState The state of the superclass of this view
     */
    protected AbsSavedState(@NotNull ParcelableEx superState) {
        if (superState == null) {
            throw new IllegalArgumentException("superState must not be null");
        }
        mSuperState = superState != EMPTY_STATE ? superState : null;
    }

    /**
     * Constructor used when reading from a parcel. Reads the state of the superclass.
     *
     * @param source parcel to read from
     */
    protected AbsSavedState(@NotNull Parcel source) {
        this(source, null);
    }

    /**
     * Constructor used when reading from a parcel. Reads the state of the superclass.
     *
     * @param source parcel to read from
     * @param loader ClassLoader to use for reading
     */
    protected AbsSavedState(@NotNull Parcel source, @Nullable ClassLoader loader) {
        ParcelableEx superState = source.readParcelableEx(loader);
        mSuperState = superState != null ? superState : EMPTY_STATE;
    }

    /**
     * getSuperState
     * @return Parcelable
     */
    @Nullable
    public final ParcelableEx getSuperState() {
        return mSuperState;
    }

    @Override
    public void marshallingEx(Parcel parcel) throws ParcelException {
        parcel.writeString(AbsSavedState.class.getName());
        parcel.writeParcelableEx(mSuperState);
    }

    @Override
    public void unmarshallingEx(Parcel in) throws ParcelException {
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
        try {
            classLoader.loadClass(AbsSavedState.class.getName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        mSuperState = in.readParcelableEx(classLoader);
        if (mSuperState != null) {
            throw new IllegalStateException("superState must be null");
        }
    }
}
